<?php

/**
 * This is the model class for table "users".
 *
 * The followings are the available columns in table 'users':
 * @property integer $id
 * @property string $username
 * @property string $full_name
 * @property string $password
 * @property string $email
 * @property integer $active
 * @property string $avatar
 * @property string $location
 * @property string $website
 * @property string $bio
 * @property integer $public
 * @property string $created_date
 *
 * The followings are the available model relations:
 * @property Follows[] $follows
 * @property Follows[] $follows1
 * @property Mentions[] $mentions
 * @property Refavs[] $refavs
 * @property Timeline[] $timelines
 * @property Tweets[] $tweets
 */
class TUsers extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Users the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'users';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('email','email', 'message'=>Html::tag(
	            'p', 
	            array(
	                'class'=>'error active',
	                'role' =>'alert',
	            ),
	            "Doesn't look like a valid email."
	        	)
			),
			array('username, email', 'unique'),
			array('username, full_name, password, email, created_date', 'TRequiredValidator'),
			array('active', 'numerical', 'integerOnly'=>true),
			array('username, password', 'length', 'max'=>50),
			array('full_name', 'length', 'max'=>250),
			array('email', 'length', 'max'=>25),
			array('avatar, bio', 'length', 'max'=>255),
			array('location, website', 'length', 'max'=>150),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, username, full_name, password, email, active, avatar, location, website, bio, created_date', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'follows' => array(self::HAS_MANY, 'Follows', 'user_id'),
			'follows1' => array(self::HAS_MANY, 'Follows', 'follow_id'),
			'mentions' => array(self::HAS_MANY, 'Mentions', 'user_id'),
			'refavs' => array(self::HAS_MANY, 'Refavs', 'user_id'),
			'timelines' => array(self::HAS_MANY, 'Timeline', 'user_id'),
			'tweets' => array(self::HAS_MANY, 'Tweets', 'user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'username' => 'Username',
			'full_name' => 'Full Name',
			'password' => 'Password',
			'email' => 'Email',
			'active' => 'Active',
			'avatar' => 'Avatar',
			'location' => 'Location',
			'website' => 'Website',
			'bio' => 'Bio',
			'public' => 'Public',
			'created_date' => 'Created Date',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('full_name',$this->full_name,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('active',$this->active);
		$criteria->compare('created_date',$this->created_date,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	protected function afterValidate() 
	{ 
		if ($this->getErrors() == array()) 
		{
			return;
		}
		$this->addSuccessMessage('full_name');
		$this->addSuccessMessage('email');
		$this->addSuccessMessage('password');
		$this->addSuccessMessage('username');
	    $this->onAfterValidate(new CEvent($this)); 
	}

	private function addSuccessMessage($attribute)
	{
		if ($this->getError($attribute) != null) return;
		switch ($attribute) {
			case 'full_name':
				$successMessage = '<p role="alert" class="isaok active">Name looks great</p>';
				break;
			case 'email':
				$successMessage = '<p role="alert" class="isaok active">We will email you a confirmation.</p>';
				break;
			case 'password':
				$successMessage = '<p role="alert" class="isaok active">Password is perfect!</p>';
				break;
			case 'username':
				$successMessage = '<p role="alert" class="isaok active">You can change it later.</p>';
				break;
			
			default:
				$successMessage = "";
				break;
		}
		if ($successMessage !="")
			$this->addError($attribute, $successMessage);
	}
}